﻿using DataWasher.WebApi.Repositories;
using Microsoft.Azure.Cosmos.Table;
using Report.WebApi.Interfaces;
using Report.WebApi.Models;
using Report.WebApi.Models.ResponseModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToolManager.Entities;
using ToolManager.IService;

namespace Report.WebApi.Services.Common
{
    public class CommonService : QueryService, ICommonService
    {
        private readonly ISysDomainRepository _repository;
        private readonly IStorageService _storageService;
        private readonly ISysDomainRepository _domainRepository;
        public CommonService(ISysDomainRepository repository, IStorageService storageService, ISysDomainRepository domainRepository) : base(domainRepository)
        {
            _repository = repository;
            _storageService = storageService;


        }
        /// <summary>
        /// 获取组织
        /// </summary>
        /// <param name="OwnerId"></param>
        /// <returns></returns>
        public async Task<List<RegionModel>> GetRegions(string OwnerId)
        {
            RegionModel model = null;
            var regionList = new List<RegionModel>();
            var domainModellst = await _repository.GetDomainList(OwnerId, "[dbo].[DGDomainNx]");
            var firstLevels = domainModellst.Where(x => x.Level == 1).ToList();
            foreach (var level in firstLevels)
            {
                model = new RegionModel();
                model.RegionName = level.DomainName;
                model.DomainId = level.DomainId.ToLower();
                model.ChildRegion = DGGetChildRegion(level.DomainId, domainModellst);
                regionList.Add(model);

            };
            return regionList;
        }

        public List<RegionModel> DGGetChildRegion(string domainId, List<DomainModel> domainList)
        {
            var regionList = new List<RegionModel>();
            RegionModel rgm = null;
            var selectedDomains = domainList.Where(x => x.ParentDomainId == domainId).ToList();
            var level = domainList.Max(x => x.Level);
            foreach (var domain in selectedDomains)
            {
                if (level == domain.Level || domain.DomainTag == "店铺")
                    continue;
                rgm = new RegionModel();
                rgm.DomainId = domain.DomainId;
                rgm.RegionName = domain.DomainName;
                rgm.ChildRegion = DGGetChildRegion(domain.DomainId, domainList);
                regionList.Add(rgm);
            };
            return regionList;
        }


        /// <summary>
        /// 获取店铺标签
        /// </summary>
        /// <param name="OwnerId"></param>
        /// <returns></returns>
        public async Task<List<TagNameResponse>> GetTags(string OwnerId)
        {
            var query = new TableQuery<TagNameEntity>();
            var tagNamesLst = await _storageService.GetBusData("TagName", query);
            var domainIds = await GetDomainIds(new QueryRequestModel() { OwnerId = OwnerId });
            var tagNameFilterlst = tagNamesLst.Where(x => domainIds.Contains(x.DomainId)).ToList();
            var result = tagNameFilterlst.Select(x => new TagNameResponse() { TagName = x.TagName }).Select(x => x.TagName).Distinct().ToList();
            var response = new List<TagNameResponse>();
            result.ForEach(t =>
            {
                response.Add(new TagNameResponse() { TagName = t });
            });
            return response;
        }

        /// <summary>
        /// 获取优先级
        /// </summary>
        /// <param name="OwnerId"></param>
        /// <returns></returns>
        public async Task<List<PriorityResponse>> GetPriority(string OwnerId, string lang)
        {
            var query = new TableQuery<PriorityEntity>().Where($"(OwnerId eq '{OwnerId}') and (Language eq '{lang}')");
            var priorityLst = await _storageService.GetBusData("Priority", query);
            var response = priorityLst.Select(x => new PriorityResponse() { DataCode = x.DataCode, Content = x.Content, Language = int.Parse(x.Language) }).Distinct().ToList();
            return response;
        }

        /// <summary>
        /// 获取维修类别
        /// </summary>
        /// <param name="OwnerId"></param>
        /// <returns></returns>
        public async Task<List<CategoryResponse>> GetCategory(string OwnerId, string lang)
        {
            var query = new TableQuery<MntCategoriesEntity>().Where($"(OwnerId eq '{OwnerId}') and (Language eq '{lang}')");
            var categoryLst = await _storageService.GetBusData("MntCategory", query);
            var response = categoryLst.Select(x => new CategoryResponse() { Language = int.Parse(x.Language), Content = x.Content }).Distinct().ToList();
            return response;
        }


    }
}
